package puzzle.projecteuler.p100;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import astudy.util.Permutation;

public class Problem032 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		Set<Integer> r = new HashSet<Integer>();
		List<int[]> s = go();
		for (int[] a: s) {
			r.add(a[2]);
		}
		int P = 0;
		for (int p: r) {
			P += p;
		}
		System.out.println(P);
	}

	public static List<int[]> go() {
		
		List<int[]> solutions = new ArrayList<int[]>();
		int[] c = {1,2,3,4,5,6,7,8,9};
		while (c != null) {
			Integer[] a = new Integer[9];
			for (int i = 0; i < 9; i ++) a[i] = c[i];
			String b = StringUtils.join(a, "");
			for (int i = 1; i < 9; i ++) {
				for (int j = i+1; j < 9; j ++) {
					int x = Integer.valueOf(b.substring(0, i));
					int y = Integer.valueOf(b.substring(i, j));
					int z = Integer.valueOf(b.substring(j));
					if (x*y == z) {
						solutions.add(new int[] {x,y,z});
					}
				}
			}
			c = Permutation.next(c);
		}

		return solutions;
	}
}
